#include "gui/collapsiblewidget.h"
#include "core/constants.h"

QString CollapsibleWidget::getText()
{
    return this->headerLabel->text();
}

void CollapsibleWidget::setText(QString text)
{
    this->headerLabel->setText(text);
}

QWidget *CollapsibleWidget::getWidget()
{
    return this->expandWidget;
}

void CollapsibleWidget::toggleWidget()
{
    if (this->expandWidget == NULL) {
        return;
    }

    if (this->expandWidget->isHidden()) {
        this->expandWidget->show();
        this->collapseButton->setText("Hide");
    } else {
        this->expandWidget->hide();
        this->collapseButton->setText("Show");
    }
}

void CollapsibleWidget::setWidget(QWidget *widget)
{
    if (this->expandWidget) {
        delete this->expandWidget;
    }

    this->expandWidget = widget;
    this->mainLayout->addWidget(widget);
}

void CollapsibleWidget::initUiComponents()
{
    QFont font;
    mainLayout = new QVBoxLayout;
    topLayout = new QHBoxLayout;

    headerLabel = new QLabel;
    font = headerLabel->font();
    font.setPointSize(font.pointSize() + HEADER_UPSIZE - 2);
    headerLabel->setFont(font);
    topLayout->addWidget(headerLabel);

    separator = new SeparatorWidget;
    topLayout->addWidget(separator, 1);

    collapseButton = new QPushButton("Hide");
    collapseButton->setMaximumWidth(40);
    topLayout->addWidget(collapseButton);
    mainLayout->addLayout(topLayout);
    this->setLayout(mainLayout);
}

CollapsibleWidget::CollapsibleWidget(QWidget *parent) :
    QWidget(parent)
{
    this->initUiComponents();
    this->expandWidget = NULL;
    connect(this->collapseButton, SIGNAL(clicked()),
            this, SLOT(toggleWidget()));
}
